package v1

import (
	"gin-example/models"
	"gin-example/pkg/e"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"log"
	"net/http"
	"strconv"
)

func GetEmploymentsForUser(c *gin.Context) {
	data := make(map[string]interface{})
	id := c.Query("id")
	valid1 := validation.Validation{}
	valid1.Required(id, "id").Message("用户id不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.SUCCESS
		tmp, _ := strconv.Atoi(id)
		data["employers"] = models.GetEmployers(tmp)
		data["employees"] = models.GetEmployees(tmp)
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func GetEmploymentsForStation(c *gin.Context) {
	data := make(map[string]interface{})
	id := c.Query("id")
	valid1 := validation.Validation{}
	valid1.Required(id, "id").Message("储能站id不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.SUCCESS
		tmp, _ := strconv.Atoi(id)
		data["employments"] = models.GetEmploymentsForStation(tmp)
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func AddEmployment(c *gin.Context) {
	json := make(map[string]string)
	_ = c.BindJSON(&json)

	employerId := json["employerId"]
	employeeId := json["employeeId"]
	stationId := json["stationId"]
	valid1 := validation.Validation{}
	valid1.Required(employerId, "employerId").Message("employerId不能为空")
	valid1.Required(employeeId, "employeeId").Message("employeeId不能为空")
	valid1.Required(stationId, "stationId").Message("stationId不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		tmp1, _ := strconv.Atoi(employerId)
		tmp2, _ := strconv.Atoi(employeeId)
		tmp3, _ := strconv.Atoi(stationId)
		if models.ExistUserByID(tmp1) && models.ExistUserByID(tmp2) && models.ExistStationByID(tmp3) {
			if models.ExistEmploymentByInfo(tmp1, tmp2, tmp3) {
				code = e.ERROR_ALREADY_EXIST
			} else {
				code = e.SUCCESS
				models.AddEmployment(tmp1, tmp2, tmp3)
			}
		}
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
		log.Println(json)
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func UpdateEmployment(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	code := e.INVALID_PARAMS

	if models.ExistEmploymentById(id) {
		code = e.SUCCESS
		json := make(map[string]interface{})
		_ = c.BindJSON(&json)
		employeeId := json["employeeId"]
		if employeeId != nil {
			tmp2, _ := strconv.Atoi(employeeId.(string))
			json["employeeId"] = tmp2
		}
		models.UpdateEmployment(id, json)
	} else {
		code = e.ERROR_NOT_EXIST
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func DeleteEmployment(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	code := e.INVALID_PARAMS
	if models.ExistEmploymentById(id) {
		models.DeleteEmployment(id)
		code = e.SUCCESS
	} else {
		code = e.ERROR_NOT_EXIST
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}
